package com.hax2max.control.externalclient;

import android.util.Log;

import com.hax2max.R;
import com.hax2max.control.DataHandler;
import com.hax2max.model.ExternalData;
import com.hax2max.model.Profile;
import com.hax2max.control.FriendStalkerException;

public class ClientFactory {

	public static IExternalClient getTwitterClient(Profile profile)
	{
		for(ExternalData ed: profile.externalData)
		{
			// look for our twitter profile date
			if(ed.application == ExternalData.ApplicationIDTwitter) 
				if(ed.password != null && ed.login != null && !ed.password.equals("") &&
						!ed.login.equals(""))
					return new TwitterClient(ed.login, ed.password);
		}
		Log.e("ClientFactory", "Created a limited twitter client, no possibility to set own tweets\n"); //TODO!!!! remove this line
		return new TwitterClient(); // returns a limited client without the possibility to set tweets

	}
	

	public static IExternalClient getFriendStalkerClient(Profile profile)
		{
			return new FriendStalkerClient();
		}
	
	
	public static IExternalClient getFacebookClient(Profile profile) throws FriendStalkerException.IExternalClient
	{
		
		for(ExternalData ed: profile.externalData) 
		{
			// look for our twitter profile date
			if(ed.application == ExternalData.ApplicationIDFacebook) 
				if(ed.password != null && ed.login != null && !ed.password.equals("") &&
						!ed.login.equals("") )
					return new FacebookClient(ed.login, ed.password);
		}
		Log.e("ClientFactory", "no user/pass");
		
		throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientNoUserPass);
	}

	
	
	/**
	 * Get's the Client with the applicationId, uses login
	 * instantiate and returns the ExternalClient.
	 * 
	 * @param applicationID
	 * @return IExternalClient
	 */
	public static IExternalClient getClient(int applicationId) throws FriendStalkerException.IExternalClient{
		
		Log.e("ClientFactory", "Trying to get a factory for client (id): " + applicationId +"\n");
		switch(applicationId) {
			case ExternalData.ApplicationIDFriendStalker:
				return getFriendStalkerClient(DataHandler.ownprofile);
			case ExternalData.ApplicationIDTwitter: 
				return getTwitterClient(DataHandler.ownprofile);
			case ExternalData.ApplicationIDFacebook: 
				return getFacebookClient(DataHandler.ownprofile);
		}
		throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientInvalidApplicationID);
	}
	
	
	public static IExternalClient getClient(int applicationId, String login, String password) throws FriendStalkerException.IExternalClient
	{
		Profile p = new Profile();
		p.externalData = new ExternalData[1];
		p.externalData[0] = new ExternalData();
		p.externalData[0].application = applicationId;
		p.externalData[0].login = login;
		p.externalData[0].password = password;
		
		switch (applicationId)
		{
		case ExternalData.ApplicationIDFriendStalker:
			return getFriendStalkerClient(p);
		case ExternalData.ApplicationIDTwitter: 
			return getTwitterClient(p);
		case ExternalData.ApplicationIDFacebook: 
			return getFacebookClient(p);
		default:
		throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientInvalidApplicationID);
		}
		
		
	}
}

	
